package cn.xaut.剑指offer.m50;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class demo56 {

    public int[] singleNumbers(int[] nums) {

        int[] res = new int[2];
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (set.contains(num))
                set.remove(num);
            else
                set.add(num);
        }
        int i = 0;
        for (int x : set)
            res[i++] = x;
        return res;
    }

    public int singleNumber(int[] nums) {

        int single = 0;
        for (int x : nums)
            single ^= x;
        return single;
    }

    public static void main(String[] args) {

//        System.out.println(Arrays.toString(new demo56().singleNumbers(new int[]{4, 1, 4, 6})));
//        System.out.println(Arrays.toString(new demo56().singleNumbers(new int[]{1, 2, 10, 4, 1, 4, 3, 3})));

//        System.out.println(1 ^ 0);
//        System.out.println(1 ^ 2);
//        System.out.println(1 ^ 3);
//        System.out.println(1 ^ 4);
//        System.out.println(1 ^ 5);
//        System.out.println(2 ^ 2);

        System.out.println(new demo56().singleNumber(new int[]{2, 2, 1}));
        System.out.println(new demo56().singleNumber(new int[]{4, 1, 2, 1, 2}));
        System.out.println(1 ^ 4 ^ 4);  // 1
    }
}
